import gradio as gr
from apps.image_ocr_app import ocr_image
from apps.pdf_ocr_app import ocr_pdf
from apps.audio_asr import asr


with gr.Blocks(title="OCR APPS") as app:

    gr.HTML('<h1>OCR APPS</h1>')

    with gr.Tab("PDF 识别"):
        file_input = gr.File(file_types=['.pdf'])
        pdf_ocr_output = gr.Textbox()

        clear_pdf_button = gr.ClearButton([file_input, pdf_ocr_output])
        pdf_ocr_button = gr.Button("识别 PDF", variant="primary")

        pdf_ocr_button.click(ocr_pdf, inputs=file_input, outputs=pdf_ocr_output)

    with gr.Tab("Image 识别"):
        img_input = gr.Image()
        img_ocr_output = gr.Textbox()

        clear_image_button = gr.ClearButton([img_input, img_ocr_output])
        image_ocr_button = gr.Button("识别", variant="primary")

        image_ocr_button.click(ocr_image, inputs=img_input, outputs=img_ocr_output)

    with gr.Tab("语音识别"):
        gr.Markdown("# <center>🌊💕🎶 语音识别</center>")
        inp = gr.Audio(label="请上传一个音频文件", type="filepath")
        btn = gr.Button("一键开启语音识别", variant="primary")
        out = gr.Textbox(label="文字内容", interactive=True)

        btn.click(asr, inputs=[inp], outputs=[out])


app.launch(server_name='0.0.0.0', server_port=8080)